// 外部迭代器
const Iterator = (array) => {
    let current = 0;

    const next = () => current += 1;

    const isDone = () => current >= array.length;

    const getCurrItem = () => array[ current ];

    return {
        next,
        isDone,
        getCurrItem,
        length: array.length
    }
}

const compare = (iterator1, iterator2) => {
    if (iterator1.length !== iterator2.length) {
        return false;
    }

    while(!iterator1.isDone() && !iterator2.isDone()) {
        if (iterator1.getCurrItem() !== iterator2.getCurrItem()) {
            return false;
        }
        iterator1.next();
        iterator2.next();
    }

    return true;
}

let iterator1 = Iterator([1,2,3]);
let iterator2 = Iterator([1,2,3]);
console.log( compare(iterator1, iterator2) );

iterator1 = Iterator([1,2,3]);
iterator2 = Iterator([1,2,'3']);
console.log( compare(iterator1, iterator2) );